home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’96 / Sessions ’96 / Random Numbers / Random.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  2.5 KB  |  97 lines  |  [TEXT/CWIE]

  1. #ifndef RANDOM_H
  2. #define RANDOM_H 1
  3. //
  4. // Two-Stage Cryptographic Random Number Generator
  5. //
  6. // Copyright © 1996, Jon Callas, Eldacur Technologies. All Rights Reserved.
  7. //
  8. // You have permission to use this code in any commercial or non-commercial programs provided
  9. // you do the following:
  10. //
  11. // (1) Somewhere in your documentation, about box, etc. you state that you use Jon Callas's
  12. //     Two-Stage Random Number Generator and that it is copyright by him.
  13. // (2) If you use it in a commercial product, you send me email at jon@worldbenders.com telling me
  14. //     that you're using it. I don't have any nefarious purposes, I'm not going to sell your
  15. //     name to some mailing list, I just want to know who is using it, so I can send updates
  16. //     to you and be able to brag about the bizillion people that you use this.
  17. // (3) If you find bugs, make substantive improvements, etc. that you make a best-faith effort
  18. //     to send them to me so I can incorporate it into the code.
  19. //
  20. // That's it. That's all you have to do. It's not much to ask. It's not as odious as a copyleft
  21. // so why not do it?
  22. //
  23.  
  24. #include "shs.h"
  25.  
  26. const int HASHLEN = 20;
  27. const int TOTALENTROPY = (HASHLEN * 8 * 10);
  28.  
  29. typedef struct
  30. {
  31.     long            distillerEntropy;
  32.     long            numberPlace;
  33.     long            newPlace;
  34.     long            beenFullySeeded;
  35.     SHS_CTX            still;
  36.     unsigned char    numbers[256];
  37.     
  38. } RandomState;
  39.  
  40. class Distiller
  41. {
  42. protected:
  43.     SHS_CTX        shs;
  44.     long        totalEntropy;
  45.     
  46. public:
  47.     Distiller();
  48.     
  49.     int            AddData(void *start, long length, int decibits);
  50.     void        GetHash(unsigned char hash[HASHLEN]);
  51.     void        Init();
  52.     void        SetEntropy(long decibits)        { totalEntropy = decibits; };
  53.     int            GetEntropy(void)                { return totalEntropy; };
  54. };
  55.  
  56. class Grinder
  57. {
  58. protected:
  59.     unsigned char    numbers[256];
  60.     long            numberPlace;
  61.     long            newPlace;
  62.     
  63.     void            InitNums();
  64.  
  65. public:
  66.     Grinder();
  67.     Grinder(void *theStuff, long stuffLen);
  68.     
  69.     unsigned char    Turn();
  70.     void            AddStuff(void *theStuff, long stuffLen);
  71.     
  72.     void            GetRandom(void *thePlace, long length);
  73. };
  74.  
  75.  
  76. class Randomizer : public Distiller, public Grinder
  77. {
  78. protected:
  79.     long            busy;
  80.     void            Busy()        { busy = 1; };
  81.     void            NotBusy()    { busy = 0; };
  82.  
  83. public:
  84.     Randomizer();
  85.     Randomizer(void *seed, int seedLen);
  86.     
  87.     void            AddSeed(void *seed, int seedLen, int entropyGuessInDecibits);
  88.     unsigned char    Byte();
  89.     unsigned short    Word();
  90.     unsigned long    Long();
  91.     void            String(void *start, long byteLen) { GetRandom(start, byteLen); };
  92.     long            isBusy() { return busy; };
  93.     void            GetState(RandomState *state);
  94.     void            SetState(RandomState *state);
  95. };
  96.  
  97. #endif